package com.lee.taste.spider.sohu.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.apache.tomcat.jdbc.pool.DataSource;

import com.lee.taste.spider.SpringUtils;
import com.lee.taste.spider.sohu.entity.Brand;
import com.lee.taste.spider.sohu.entity.Corporation;
import com.lee.taste.spider.sohu.entity.Model;
import com.lee.taste.spider.sohu.entity.SaleBrand;
import com.lee.taste.spider.sohu.entity.SaleCorp;
import com.lee.taste.spider.sohu.entity.SaleModel;
public class BrandDao {

	private DataSource dataSource = (DataSource) SpringUtils.getBean("dataSource");
	
	/**
	 * 更新品牌
	 * @param list
	 * @throws SQLException
	 */
	public void batchUpdateBrand(final List<Brand> list) throws SQLException {
//		doUpdate(" truncate table sohu_car_brand ");
//		doUpdate(" truncate table sohu_car_model ");
		for(Brand b : list){
			String sql = " insert into sohu_car_brand(`id`,`name`) values('"+b.getId()+"','"+b.getName()+"') ";
			doUpdate(sql);
		}
	}
	/**
	 * 更新车型，相关品牌
	 * @param list
	 * @throws SQLException
	 */
	public void batchUpdateModel(final List<Model> list) throws SQLException {
		for(Model b : list){
			String sql = " insert into sohu_car_model values('"+b.getId()+"','"+b.getName()+"','"+b.getBrandId()+"','"+b.getCorpId()+"','"+b.getCorpName()+"') ";
			doUpdate(sql);
		}
	}
	/**
	 * 更新企业
	 * @param list
	 * @throws SQLException
	 */
	public void batchUpdateCorp(final List<Corporation> list) throws SQLException {
		if(list != null && list.size()>0){
			for(Corporation entity : list){
				doUpdate(entity.toInsertString());
			}
		}
	}
	
	/**
	 * 
	 * @param sql
	 * @throws SQLException
	 */
	private void doUpdate(String sql) throws SQLException{
		Connection connection = dataSource.getConnection();
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.executeUpdate();
		ps.close();
		connection.close();
	}
	public void batchUpdateCorpSales(List<SaleCorp> list) throws SQLException {
		if(list != null && list.size()>0){
			for(SaleCorp entity : list){
				doUpdate(entity.toInsertString());
			}
		}
		
	}
	public void batchUpdateBrandSales(List<SaleBrand> list) throws SQLException {
		if(list != null && list.size()>0){
			for(SaleBrand entity : list){
				doUpdate(entity.toInsertString());
			}
		}
	}
	public void batchUpdateModelSales(List<SaleModel> list) throws SQLException {
		if(list != null && list.size()>0){
			for(SaleModel entity : list){
				doUpdate(entity.toInsertString());
			}
		}
		
	}
}

